package com.huwaiwai001.netty.nio;

import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author 歪大哥😁
 * @date 2020-07-07.
 */
public class FileChannelReader {

    public static void main(String[] args) throws Exception{
        // 创建一个输出流
        try (FileInputStream fileInputStream = new FileInputStream("/Users/kathy/studyCode/common/src/main/java/com/huwaiwai001/netty/nio/file01.txt")) {
            // 通过输出流 获取文件channel
            FileChannel channel = fileInputStream.getChannel();
            // 创建一个缓冲区
            ByteBuffer byteBuffer = ByteBuffer.allocate(1024);

            // 1.从channel里面读取数据 写入buffer
            channel.read(byteBuffer);
            // 2.从buffer读取写入的数据
            // 要切换读模式
            byteBuffer.flip();
            // 3.用byte数组接收数据
            byte[] data = new byte[byteBuffer.limit()];
            byteBuffer.get(data);
            System.out.println(new String(data));
        }
    }
}
